<?php


namespace app\common\exception33;
use think\exception\Handle;
use think\facade\Log;
use think\Response;
use Throwable;
use Exception;

class ExceptionHandler extends Handle
{
    public $httpStatus =500 ;
    private $code;
    private $msg;
    private $errorCode;

    public function render($request, Throwable $e): Response
    {
        if ($e instanceof BaseException) {
            $this->code = $e->code;
            $this->msg = $e->msg;
            $this->errorCode = $e->errCode;
        } else {
            if (env('APP_DEBUG', '0')) {
                return parent::render($request, $e);
            } else {
                $this->code = 500;
                $this->msg = '服务器错误,请联系客服或者稍后再试';
                $this->errorCode = 999;
                $this->recordErrorLog($e);
                //  dump($abc); exit;
            }
        }
        $result = [
            'errorCode' => $this->errorCode,
            'request_url' => $request->url(),
        ];
        return json(['code' => $this->code, 'msg' => $this->msg]);
    }

    //记录日志
    public function recordErrorLog(Exception $e)
    {
        Log::record($e->getMessage(), 'error');
    }

}